package com.atguigu.springcloud.cloudsentinelgatewaydevelop.service.impl;


import com.atguigu.springcloud.cloudsentinelgatewaydevelop.bean.DO.SentinelFlowRuleDO;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.bean.dto.SentinelFlowRuleDTO;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.constants.TableNameConstants;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.mapper.FlowRuleMapper;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.service.FlowRuleService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.r2dbc.spi.ConnectionFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.data.r2dbc.core.DatabaseClient;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;

import static org.springframework.data.r2dbc.query.Criteria.where;

/**
 * xxx
 *
 * @author BugFree Jin
 * @version 1.0
 * @since 2022/3/3 9:51
 */
@Service
@RequiredArgsConstructor
public class FlowRuleServiceImpl implements FlowRuleService {

    private final FlowRuleMapper flowRuleMapper;
    private final ConnectionFactory connectionFactory;
    private DatabaseClient client = null;

    @PostConstruct
    void init() {
        this.client = DatabaseClient.create(connectionFactory);
    }

    private SentinelFlowRuleDO copyField(SentinelFlowRuleDTO sentinelFlowRuleDTO) {
        SentinelFlowRuleDO sentinelFlowRuleDO = new SentinelFlowRuleDO();
        SentinelFlowRuleDO.FlowRule flowRule = new SentinelFlowRuleDO.FlowRule();
        BeanUtils.copyProperties(sentinelFlowRuleDTO, flowRule);
        BeanUtils.copyProperties(sentinelFlowRuleDTO, sentinelFlowRuleDO, "flowRule");
        String flowRuleStr = null;
        try {
             flowRuleStr = new ObjectMapper().writeValueAsString(flowRule);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        sentinelFlowRuleDO.setFlowRule(flowRuleStr);
        return sentinelFlowRuleDO;
    }

    @Override
    public Mono<Integer> save(SentinelFlowRuleDTO sentinelFlowRuleDTO) {
        SentinelFlowRuleDO sentinelFlowRuleDO = copyField(sentinelFlowRuleDTO);

        return client.insert()
                .into(TableNameConstants.FLOWRULE)
                .nullValue("id", Long.class)
                .value("application_name", sentinelFlowRuleDO.getApplicationName())
                .value("resource", sentinelFlowRuleDO.getResource())
                .value("flow_rule", sentinelFlowRuleDO.getFlowRule())
                .fetch()
                .rowsUpdated();
    }

    @Override
    public Mono<Integer> deleteById(Long id) {
        return flowRuleMapper.deleteByID(id);
    }

    @Override
    public Mono<Integer> deleteByAyApplicationName(String applicationName) {
        return flowRuleMapper.deleteByAyApplicationName(applicationName);
    }

    @Override
    public Mono<Integer> updateById(SentinelFlowRuleDTO sentinelFlowRuleDTO, Long id) {
        SentinelFlowRuleDO sentinelFlowRuleDO = copyField(sentinelFlowRuleDTO);
        return client.update()
                .table(SentinelFlowRuleDO.class)
                .using(sentinelFlowRuleDO)
                .matching(where("id").is(id))
                .fetch()
                .rowsUpdated();
    }

    @Override
    public Mono<SentinelFlowRuleDTO> findById(Long id) {
        return client.select()
                .from(TableNameConstants.FLOWRULE)
                .matching(where("id").is(id))
                .as(SentinelFlowRuleDO.class)
                .fetch()
                .one()
                .map(SentinelFlowRuleDO::getSentinelFlowRuleDTO)
                .log();
    }

    @Override
    public Flux<SentinelFlowRuleDTO> findByApplicationName(String applicationName) {
        return client.select()
                .from(TableNameConstants.FLOWRULE)
                .matching(where("application_name").is(applicationName))
                .as(SentinelFlowRuleDO.class)
                .fetch()
                .all()
                .map(SentinelFlowRuleDO::getSentinelFlowRuleDTO)
                .log();
    }
}
